var params= new Array();
webix.ready(function(){
  var clickColumnId;
  var clickRowId;
  webix.Date.startOnMonday = true;

  var calendar_window=webix.ui({
    view:"window",
    id:"my_window",
    position:'center',
    move:true,
    head:{
      view:"toolbar",margin:-4,cols:[
        {},
        { view:"icon", icon:"times-circle",click:"$$('my_window').hide();"}
      ]
    },
    body:{
      view:"calendar",
      id:"my_calendar",
      type:"time",
    }, 
  });
  var my_template={
      template: "<span style='font-size:20px; font-weight:bold; color:#3498DB'; >班别管理</span>&nbsp;/&nbsp;&nbsp;Class",
      height:45,
      borderless:true 
  };
  var my_toolbar={
    view:"toolbar",
    elements:[           
      {view:"button",value:"添加班别",width:100,click:'addRow()'},
      {view:"button",value:"保存",width:50,click:"save"}
    ]   
  };
  var my_datatable={
    height:680,
    view:"datatable",
    id:"mydatatable",     
    columns:[
      {id:"class_id",header:["序号"],fillspace:true},
      {id:"class_name",header:["班别名称"],editor:"text",fillspace:true},
      {id:"startTime",header:"起始时间",editor:"text",fillspace:true},
      {id:"endTime",header:"截止时间",editor:"text",fillspace:true },
      // {id:"operate",header:"操作",template:"<div><span class='padding_less'><a href='#' onclick=\"delRow('#class_id#')\">删除</a></div>",fillspace:true }
    ],      
    select:true,
    editable:true,
    checkboxRefresh:true,
    tooltip:true,
    //url:当是外来数据时用这个
    url:"getAllClasses"
     //data:my_class,
  };
  var web={
      container:"body",
      type:"line",
      id:"mylayout",
      rows:[
        my_template,
        my_toolbar,
        my_datatable
      ],
  };
  webix.ui(web);//---webix.ui结束----   
  var dt=$$("mydatatable");
  /*当点击开始和截止时间列时才允许弹出日历窗口*/
  dt.attachEvent("onItemClick",function(id, e, node){  
    clickColumnId=id.column;
    clickRowId=id.row;
    console.log(node);
    console.log("上："+clickColumnId);
    var timeNode = dt.getItemNode(id);

    if(clickColumnId=="startTime"||clickColumnId=="endTime"){
      $$("my_window").show(timeNode,{pos:"bottom"}); 
    } 
  })
  /*将所选的时间赋值给点击的单元格并刷新界面*/
  $$('my_calendar').attachEvent("onAfterDateSelect", function(date){
    var values=dt.getItem(clickRowId);
      var format = webix.Date.dateToStr("%H:%i:00");
      var times = format(date); //2012.05
          values[clickColumnId] = times;
      dt.refresh();    
  });
  /*点击done按钮后，直接关闭window窗口*/
  $$("my_calendar").attachEvent("onChange", function(date){
    $$('my_window').hide(); 
  });
/*------all end---------*/
});             
/*删除一行班别*/
 /* function del(){
    var dt=$$("mydatatable");
    var id=dt.getSelectedId();
    dt.remove(id);
    console.log("2b");
  }*/
function delRow(class_id){
  webix.confirm({
    title:"提示",
    text:"确定要删除吗?",
    ok:"是", 
    cancel:"否",
    callback:function(result){
      if(result==true){
        var params = {};
        params.class_id = class_id;
        webix.ajax().post("delClass", params, function(text, xml, xhr){
          alert("执行完毕");
          location.reload();
        });
      }
    }
  }); 
};
/*添加一行工序*/
function addRow(){ 
  var dt=$$("mydatatable");
  var id=dt.add({
  }); 
  dt.addRowCss(id, "newRow");
  dt.edit({
        row:id,
        column:"class_name"
});
  dt.focusEditor(); 
};

/*保存班别数据*/
function save(){
  var dt=$$("mydatatable");
  var ps = {};
  var arr=new Array();
  var i=0;
  dt.eachRow(function(row){
    var rows=dt.getItem(row);
    var r={};
    r["class_id"]=rows.class_id;
    r["class_name"]=rows.class_name;
    r["startTime"]=rows.startTime;
    r["endTime"]=rows.endTime;
    arr[i]=r;
    i++;
    params.push(rows.class_id,rows.class_name,rows.startTime,rows.endTime);
  });
  console.log(arr);
  
  ps["data"]=arr;
  
  postData("addClasses", ps,callback2);
}

function callback2(data){
	
  $$("mydatatable").clearAll();
  $$("mydatatable").define("url", "getAllClasses");
  $$("mydatatable").refresh();
}

  

